import api from '../../../api/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getdata: {},
    list: [{ "value": "20" }, { "value": "30" }, { "value": "40" }],
    once: 'change',
    loading: true,
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中...'
    })
    this.getSportsData()
    // const { once } = options
    // if (options.once) {
    //   this.setData({
    //     once
    //   });
    // }
  },
  toggleCheckbox: function (e) {
    const value = e.currentTarget.dataset.value;
    console.log(this.data.list);
    let list = this.data.list.map(item => {
      if (item.value === value) {
        item.checked = !item.checked;
      } else {
        item.checked = false;
      }
      return item;
    });
    this.setData({
      list: list
    });
  },
  putData() {
    console.log('tijiao', this.data.list);
    const array = this.data.list
    let num = []
    for (let j = 0; j < array.length; j++) {
      if (array[j].checked == true) {
        num.push(array[j].value)
      }
    }
    console.log(num);
    num = num.toString(num)
    console.log(num);
    this.put(num)
  },
  async getSportsData() {
    try {
      const res = await api.post('/api/user/storage/get', {
        data: {
          'key': 'hxhealthsporttags'
        }
      })

      let array = res[0].value
      const list = this.data.list;
      console.log(array);
      if (typeof array === 'string') {
        array = JSON.parse(array)
        if(array && typeof array === 'object' && !Array.isArray(array)) {
          this.setData({
            getdata: array
          });
          console.log(array, '----------------------');
          if (array.mtiontime) {
            // 有选择
            console.log('222');
            if (array.mtiontime) {
              let tagsList = array.mtiontime.split(',')
              list.forEach(item => {
                list.checked = false
                tagsList.forEach(tag => {
                  if (tag == item.value) {
                    item.checked = true;
                  }
                })
              })
              this.setData({
                list: list,
                loading: false
              });
              wx.hideLoading()
            }
          } else {
            this.setData({
              loading: false
            });
            wx.hideLoading()
          }
        } else {
          this.setData({
            loading: false
          });
          wx.hideLoading()
        }
      } else {
        this.setData({
          loading: false
        });
        wx.hideLoading()
      }
    } catch (e) {
      console.log('get->', e)
      return null
    }
  },
  async put(result) {
    console.log(result);
    console.log(this.data.getdata);
    let getdata = this.data.getdata
    getdata.mtiontime = result
    console.log(getdata);
    this.setData({
      getdata: getdata
    });
    // let lala = {"tags":"减脂减重,疾病康养","shape":"直筒型","injuryillness":"膝盖","mtiontime":"30"}
    try {
      const res = await api.post('/api/user/storage/set', {
        data: {
          'key': 'hxhealthsporttags',
          'value': JSON.stringify(getdata)
        }
      })
      wx.showToast({
        title: '保存成功',
        icon: 'none',
        duration: 3000
      })
      wx.navigateTo({ url: '/sportsClass/pages/change/index' });
      console.log(res);
      return res
    } catch (e) {
      console.log('put->', e)
      return null
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})